import Vue from 'vue'
import VueRouter from 'vue-router'
import { getItem } from '@/utils/token'
import userssionRouter from './components/userssion' 
Vue.use(VueRouter)

const routes = [
  { path: '/',redirect:'/login'},
  { path: '/login', name: 'login', component: () => import("@/views/login"),},
  { path: '/404',name: 'error', component: () => import('@/404.vue')},
  { path: '/home',redirect:'/shopList'},
  { path: '/home',name: 'home', component: () => import("@/views/home"),
  children: [{
    path: '/shopList',
    name: 'shopList',
    component: () => import('@/views/shop/index'),
    meta: { title: '信息总览', icon: 'el-icon-s-promotion' }
  }]}, 
]

 // 由于路由少显得这里这样写呆呆的
 const routerList = [
  userssionRouter
]
const router = new VueRouter({
  // 此处注释开发模式下别解开注释
  // mode: 'history',
  // base: '/hr/', // 配置项目的基础地址
  scrollBehavior: () => ({ y: 0 }), // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
  routes:[...routes,...routerList]
})
// 处理路由重定向后导航重复问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
// 控制权限
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (to.matched.length === 0) return next('/404')// 判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页
  const token = getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
